<?php

namespace App\Http\Controllers\CmsApi;

use App\Http\Model\HopeLuckDraw;
use App\Http\Model\LuckDraw;
use App\Http\Model\WishDefender;
use App\Http\Model\WishWheel;
use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\SystemServices;
use App\Http\Services\ContentCheckServices;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class InnerSystemController extends BaseController
{
    protected $systemService;
    protected $contentCheckService;
    protected $grasslandsheepRuleKey = 'GRASSLANDSHEEP:GAMERULEZQ';

    public function __construct()
    {
        $this->systemService = new SystemServices();
        $this->contentCheckService = new ContentCheckServices();

    }

    /**
     * 获取阿里云验证码配置
     * */
    public function getAliVerifyConfig(Request $request)
    {
        $data = $this->systemService->getAliVerify();

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 阿里云验证码校验
     * */
    public function aliVerify(Request $request)
    {
        $captchaVerifyParam = $request->input('captchaVerifyParam');
        $phone = $request->input('phone');
        if (empty($captchaVerifyParam)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $result = $this->systemService->aliVerify($captchaVerifyParam, $phone);

        Response::returnResult($result['code'], $result['data'], $result['msg']);
    }

    /**
     * 文字机审测试
     * */
    public function auditContentCeshi(Request $request)
    {
        $content = $request->input('content');
        $result = $this->contentCheckService->auditContentCeshi($content);

        print_r($result);
        exit;
    }

    /**
     * 文字机审
     * */
    public function auditContent(Request $request)
    {
        $content = $request->input('content');
        $userId = $request->input('userId');
        $result = $this->contentCheckService->auditContent($userId, $content);

        Response::returnResult(Hint::SUCCESS, $result);
    }

    /**
     * 添加黑名单语言
     */
    public function addBlackWord(Request $request)
    {
        $param['word_desc'] = str_replace(' ', '', $request->input("word_desc"));
        $param['admin_id'] = (int)$request->input("admin_id");
        $param['promptly_type'] = (int)$request->input("promptly_type");
        if (empty($param['word_desc']) || empty($param['promptly_type']) || empty($param['admin_id'])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->systemService->addBlackWord($param);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 编辑黑名单语言
     */
    public function editBlackWord(Request $request)
    {
        $id = $request->input("id");
        $param['word_desc'] = str_replace(' ', '', $request->input("word_desc"));
        $param['promptly_type'] = (int)$request->input("promptly_type");
        $param['admin_id'] = (int)$request->input("admin_id");
        if (empty($id) || empty($param['word_desc']) || empty($param['promptly_type']) || empty($param['admin_id'])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->systemService->editBlackWord($id, $param);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 删除黑名单语言
     */
    public function delBlackWord(Request $request)
    {
        $id = $request->input("id");
        $param['admin_id'] = $request->input("admin_id");
        if (empty($id) || empty($param['admin_id'])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->systemService->delBlackWord($id, $param);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }


    public function updateGameRule(Request $request)
    {
        if (!$request->post("gameRules")) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        if (!$request->post("type")) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $gameRule = $request->post("gameRules");
        $type = $request->post("type");

        switch ($type) {
            case "xp":
                $luckDraw = new LuckDraw();
                $gameRule = ['gameRule' => $gameRule];
                $luckDraw->updateGameRule($gameRule);
                break;
            case "dp":
                $luckDraw = new WishWheel();
                $gameRule = ['gameRule' => $gameRule];
                $luckDraw->updateGameRule($gameRule);
                break;
            case "wl":
                $luckDraw = new HopeLuckDraw();
                $gameRule = ['gameRule' => $gameRule];
                $luckDraw->updateGameRule($gameRule);
                break;
            case "lz":
                $luckDraw = new WishDefender();
                $gameRule = ['gameRule' => $gameRule];
                $luckDraw->updateGameRule($gameRule);
                break;
            case "bl":
                $this->updateRule($gameRule);
                break;
        }

        Response::returnResult(Hint::SUCCESS, []);
    }

    public function updateRule($gameRule)
    {
        Redis::set($this->grasslandsheepRuleKey, $gameRule);
    }

    //TODO 倍率游戏规则
    public function getGrassSheepRule(Request $request)
    {
        $res = Redis::get($this->grasslandsheepRuleKey) ?? "";
        Response::returnResult(Hint::SUCCESS, ['rule' => $res]);
    }

    public function getGameRule(Request $request)
    {
        if (!$request->get("type")) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $type = $request->get("type");

        switch ($type) {
            case "xp":
                $luckDraw = new LuckDraw();
                $res = $luckDraw->getGameRule();
                break;
            case "dp":
                $luckDraw = new WishWheel();
                $res = $luckDraw->getGameRule();
                break;
            case "wl":
                $luckDraw = new HopeLuckDraw();
                $res = $luckDraw->getGameRule();
                break;
            case "lz":
                $luckDraw = new WishDefender();
                $res = $luckDraw->getGameRule();
                break;
            case "bl":
                $res = json_encode(['rule' => (Redis::get($this->grasslandsheepRuleKey) ?? "")], JSON_UNESCAPED_UNICODE);
                break;
        }
//        write_log("gameRule", "abcd", $res);
        Response::returnResult(Hint::SUCCESS, $res);
    }
}